package com.suse.common.util.cookie;

import cn.hutool.core.util.ObjectUtil;
import com.suse.common.util.request.RequestUtil;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * Cookie工具类
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-08-30 16:29
 */
public class CookieUtil {

    public static String getValue(String name) {
        HttpServletRequest request = RequestUtil.getRequest();
        Cookie[] cookies = request.getCookies();
        if (ObjectUtil.isNotNull(cookies)) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

    public static String getValue(String name,HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (ObjectUtil.isNotNull(cookies)) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

}
